package game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Observable;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class DrawableRectangleImpl 
    extends Observable
	implements IDrawableRectangle
{
	@XmlElement
	private int x;
	@XmlElement
	private int y;
	@XmlElement
	private int width;
	@XmlElement
	private int height;

	private Color color;

	//needed for Jaxb
	public DrawableRectangleImpl()
	{
		;
	}
	
	public DrawableRectangleImpl(int x, int y)
	{
		//stateChangeListeners = new HashSet<IStateChangeListener>();
		color = Color.WHITE;
		x(x);
		y(y);
		width(1);
		height(1);
	}
	
//	protected void notifyStateChangeListeners()
//	{
//		for (IStateChangeListener stateChangeListener : stateChangeListeners)
//		{
//			if (stateChangeListener != null)
//			{
//				stateChangeListener.stateChanged(this);
//			}
//		}
//	}
	
	@Override
	public void paintComponent(Graphics g)
	{
//		if (Math.random() * 10 > 5)
//		{
//			g.setColor(Color.WHITE);
//		}
//		else
//		{
//			g.setColor(Color.BLACK);
//		}
		g.setColor(color() );
		g.fillRect(x(), y(), width(), height() );
	}
	
	//accessors
	@Override
	public int x(int x)
	{
		this.x = x;
//		notifyStateChangeListeners();
		setChanged();
        notifyObservers(this);
		return x();
	}
	
	@Override
	public int x()
	{
		return x;
	}
	
	@Override
	public int y(int y)
	{
		this.y = y;
//		notifyStateChangeListeners();
		setChanged();
        notifyObservers(this);
		return y();
	}
	
	@Override
	public int y()
	{
		return y;
	}
	
	@Override
	public int width(int width)
	{
		this.width = width;
//		notifyStateChangeListeners();
		setChanged();
        notifyObservers(this);
		return width();
	}
	
	@Override
	public int width()
	{
		return width;
	}
	
	@Override
	public int height(int height)
	{
		this.height = height;
//		notifyStateChangeListeners();
		setChanged();
        notifyObservers(this);
		return height();
	}
	
	@Override
	public int height()
	{
		return height;
	}
	
	@Override
	public Color color(Color color)
	{
		this.color = color;
		setChanged();
        notifyObservers(this);
		return color();
	}

	@Override
	public Color color()
	{
		return color;
	}
	
	@Override
	public Rectangle bounds(int x, int y, int width, int height)
	{
		x(x);
		y(y);
		width(width);
		height(height);
		setChanged();
        notifyObservers(this);
		return bounds();
	}
	
	@Override
	public Rectangle bounds()
	{
		x(x);
		y(y);
		width(width);
		height(height);
		return new Rectangle(x(), y(), width(), height() );
	}
	
	//following 2 methods are adapter for Color; preferred to XmlAdapter
	@SuppressWarnings("unused")
	@XmlElement(name="color")
	private void setColor(String color)
	{
		Color aux = null;
		try {
			aux = Color.decode(color);
		}
		catch(Throwable t)
		{
			System.out.println(t);
		}
		
		this.color = aux;
	}
	
	@SuppressWarnings("unused")
	private String getColor()
	{
		return "#"+Integer.toHexString(color().getRGB() & 0xffffff);
	}
}
